package com.mjh.zhujianju.service.base.impl;

import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.utils.DeleteType;
import com.mjh.zhujianju.dao.base.EnshrineMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.EnshrineEO;
import com.mjh.zhujianju.service.base.EnshrineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;

/**
 * @author liuyang
 * @Description: 收藏
 * @date 2018/8/14 0:34
 */
@Service
public class EnshrineServiceImpl extends BaseServiceImpl<EnshrineEO> implements EnshrineService {

    @Autowired
    private EnshrineMapper enshrineMapper;

    public EnshrineServiceImpl(EnshrineMapper enshrineMapper) {
        super(enshrineMapper);
    }


    @Override
    public String addEnshrine(EnshrineEO enshrineEO) {
        boolean b1 = enshrineEO.getType().equals(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        boolean c1 = StringUtils.isEmpty(enshrineEO.getCreateUserId());
        boolean d1 = StringUtils.isEmpty(enshrineEO.getBusinessId());
        boolean isLink = StringUtils.isEmpty(enshrineEO.getLink());

        if (!b1 || c1 || d1 || isLink){
            return "parameter";
        }

        List<EnshrineEO> list = enshrineMapper.queryIsAdd(enshrineEO);
        if (!CollectionUtils.isEmpty(list)){
            return "repeat";
        }
        enshrineEO.setCreateTime(new Date());
        enshrineMapper.insertSelective(enshrineEO);
        return "ok";
    }

    @Override
    public String revokeEnshrine(EnshrineEO enshrineEO) {
        boolean b1 = enshrineEO.getType().equals(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        boolean c1 = StringUtils.isEmpty(enshrineEO.getCreateUserId());
        boolean d1 = StringUtils.isEmpty(enshrineEO.getBusinessId());
        boolean isLink = StringUtils.isEmpty(enshrineEO.getLink());

        if (!b1 || c1 || d1 || isLink){
            return "parameter";
        }

        List<EnshrineEO> list = enshrineMapper.queryIsAdd(enshrineEO);
        if (CollectionUtils.isEmpty(list)){
            return "error";
        }
        enshrineEO = list.get(0);
        enshrineEO.setIsDel(DeleteType.IS_DELETE);
        enshrineEO.setUpdateTime(new Date());
        enshrineMapper.updateByPrimaryKey(enshrineEO);
        return "ok";
    }
}
